<?php
/*
Plugin Name: WordPress Mobile 2
Plugin URI: 
Description: Mobile version of your blog, based on an old version of Alex King's WordPress Mobile Edition plugin.
Author: Scott Allan Wallick
Author URI: http://scottwallick.com/
Version: 0.1 &beta;
*/ 

if ( !function_exists('is_admin_page') ) {
	function is_admin_page() {
		if ( function_exists('is_admin') ) {
			return is_admin();
		}
		if ( function_exists('check_admin_referer') ) {
			return true;
		} else {
			return false;
		}
	}
}

$_SERVER['REQUEST_URI'] = ( isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : $_SERVER['SCRIPT_NAME'] . (( isset($_SERVER['QUERY_STRING']) ? '?' . $_SERVER['QUERY_STRING'] : '')));

function wpm2_check_mobile() {
	if ( !isset($_SERVER["HTTP_USER_AGENT"]) || ( isset($_COOKIE['wpm2_mobile']) && $_COOKIE['wpm2_mobile'] == 'false' ) ) {
		return false;
	}
	if ( wpm2_mobile_exclude() ) {
		return false;
	}
	if ( isset($_COOKIE['wpm2_mobile']) && $_COOKIE['wpm2_mobile'] == 'true' ) {
		return true;
	}
	$whitelist = array(
		'Stand Alone/QNws'
	);
	foreach ( $whitelist as $browser ) {
		if ( strstr( $_SERVER["HTTP_USER_AGENT"], $browser ) ) {
			return false;
		}
	}
	$small_browsers = array(
		'2.0 MMP',
		'240x320',
		'400X240',
		'AvantGo',
		'BlackBerry',
		'Blazer',
		'Cellphone',
		'Danger',
		'DoCoMo',
		'Elaine/3.0',
		'EudoraWeb',
		'Googlebot-Mobile',
		'hiptop',
		'IEMobile',
		'KYOCERA/WX310K',
		'LG/U990',
		'MIDP-2.',
		'MMEF20',
		'mobileOK',
		'MOT-V',
		'NetFront',
		'Newt',
		'Nintendo Wii',
		'Nitro',
		'Nokia',
		'Opera Mini',
		'Palm',
		'PlayStation Portable',
		'portalmmm',
		'Proxinet',
		'ProxiNet',
		'SHARP-TQ-GX10',
		'SHG-i900',
		'Small',
		'SonyEricsson',
		'Symbian OS',
		'SymbianOS',
		'TS21i-10',
		'UP.Browser',
		'UP.Link',
		'webOS',
		'Windows CE',
		'WinWAP',
		'YahooSeeker/M1A1-R2D2',
	);

	foreach ( $small_browsers as $browser ) {
		if ( strstr( $_SERVER["HTTP_USER_AGENT"], $browser ) ) {
			return true;
		}
	}
	return false;
}

function wpm2_mobile_exclude() {
	$exclude = false;
	$pages_to_exclude = array(
		'wp-admin',
		'wp-comments-post.php',
		'wp-mail.php',
		'wp-login.php'
	);
	foreach ( $pages_to_exclude as $exclude_page ) {
		if ( strstr( strtolower($_SERVER['REQUEST_URI']), $exclude_page ) ) {
			$exclude = true;
		}
	}
	return $exclude;
}

function wpm2_template($theme) {
	if ( wpm2_installed() ) {
		return apply_filters( 'wpm2_template', 'wp-mobile-2' );
	} else {
		return $theme;
	}
}

function wpm2_installed() {
	return is_dir(ABSPATH . '/wp-content/themes/wp-mobile-2');
}

if ( is_admin_page() && !wpm2_installed() ) {
	global $wp_version;
	if ( isset($wp_version) && version_compare( $wp_version, '2.5', '>=' ) ) {
		add_action( 'admin_notices', create_function( '', "echo '<div class='error'>WordPress Mobile 2 is incorrectly installed.</div>';" ) );
	}
}

function wpm2_mobile_available($content) {
	return $content.'<p class="mobile information"><a href="' . get_bloginfo('wpurl') . '/index.php?ak_action=accept_mobile" title="View the mobile version of this site">Mobile Site</a> | Full Site</p>';
}

function wpm2_mobile_link() {
	echo '<a href="' . get_bloginfo('wpurl') . '/index.php?ak_action=force_mobile" title="View the mobile version of this site">Mobile Site</a>';
}

if (!function_exists('ak_recent_posts')) {
// this is based almost entirely on:
/*
Plugin Name: Recent Posts
Plugin URI: http://mtdewvirus.com/code/wordpress-plugins/
Description: Returns a list of the most recent posts.
Version: 1.07
Author: Nick Momrik
Author URI: http://mtdewvirus.com/
*/
	function wpm2_recent_posts( $count = 5, $before = '<p>', $after = '</p>', $hide_pass_post = true, $skip_posts = 0, $show_excerpts = false, $where = '', $join = '', $groupby = '' ) {
		global $wpdb;
		$time_difference = get_settings('gmt_offset');
		$now = gmdate("Y-m-d H:i:s",time());
		$join = apply_filters('posts_join', $join);
		$where = apply_filters('posts_where', $where);
		$groupby = apply_filters('posts_groupby', $groupby);
		if (!empty($groupby)) { $groupby = ' GROUP BY '.$groupby; }
		$request = "SELECT ID, post_title, post_excerpt FROM $wpdb->posts $join WHERE post_status = 'publish' AND post_type != 'page' ";
		if ($hide_pass_post) $request .= "AND post_password ='' ";
		$request .= "AND post_date_gmt < '$now' $where $groupby ORDER BY post_date DESC LIMIT $skip_posts, $count";
		$posts = $wpdb->get_results($request);
		$output = '';
		if ($posts) {
			foreach ($posts as $post) {
				$post_title = stripslashes($post->post_title);
				$permalink = get_permalink($post->ID);
				$output .= $before . '<a href="' . $permalink . '" rel="bookmark" title="Permanent Link: ' . htmlspecialchars($post_title, ENT_COMPAT) . '">' . htmlspecialchars($post_title) . '</a>';
				if($show_excerpts) {
					$post_excerpt = stripslashes($post->post_excerpt);
					$output.= '<br />' . $post_excerpt;
				}
				$output .= $after;
			}
		} else {
			$output .= $before . "None found" . $after;
		}
		echo $output;
	}
}

function wpm2_request_handler() {
	if ( isset($_GET['ak_action']) ) {
		$url = parse_url(get_bloginfo('url'));
		$domain = $url['host'];
		if (!empty($url['path'])) {
			$path = $url['path'];
		} else {
			$path = '/';
		}
		$redirect = false;
		switch ($_GET['ak_action']) {
			case 'reject_mobile':
				setcookie(
					'wpm2_mobile'
					, 'false'
					, time() + 300000
					, $path
					, $domain
				);
				$redirect = true;
				break;
			case 'force_mobile':
			case 'accept_mobile':
				setcookie(
					'wpm2_mobile'
					, 'true'
					, time() + 300000
					, $path
					, $domain
				);
				$redirect = true;
				break;
		}
		if ($redirect) {
			if (!empty($_SERVER['HTTP_REFERER'])) {
				$go = $_SERVER['HTTP_REFERER'];
			}
			else {
				$go = get_bloginfo('url');
			}
			header('Location: '.$go);
			die();
		}
	}
}
add_action( 'init', 'wpm2_request_handler' );

if (wpm2_check_mobile()) {
	add_action( 'template', 'wpm2_template' );
	add_action( 'option_template', 'wpm2_template' );
	add_action( 'option_stylesheet', 'wpm2_template' );
}

if ( isset($_COOKIE['wpm2_mobile']) && $_COOKIE['wpm2_mobile'] == 'false' ) {
	add_action( 'the_content', 'wpm2_mobile_available' );
}

?>